<?php
class AccountController extends CommonController
{
	public function index()
	{
		$accountCls = new AccountModel($this->uid);
		$account = $accountCls->getAccounts(2);
		
		$this->view->assign('title', '资产账户管理');
		$this->view->assign('AccountList', $account);
		$this->view->setPlace('content', 'place/account');
		$this->view->display();
	}
	
	/**
	 * 添加资产账户
	 */
	public function add()
	{
		$do = Functions::gpc('do', 'p');
		if($do == 'save')
		{
			$doInfo = array('SUCCESS'=>1, 'INFO'=>'', 'CODE'=>0);
			$data['UID'] = $this->uid;
			$data['AccountName'] = Functions::sqlgpc('AccountName', 'p');
			$data['InitAssets'] = Functions::sqlgpc('InitAssets', 'p');
			$data['AccountType'] = Functions::sqlgpc('AccountType', 'p');
			$data['Bank'] = Functions::sqlgpc('Bank', 'p');
			$data['Status'] = 1;
			$data['CreateDate'] = time();
			$data['Remark'] = Functions::sqlgpc('Remark', 'p');
			$accountModelCls = new AccountModel($this->uid);
			if(!$accountModelCls->add($data))
			{
				$doInfo = array('SUCCESS'=>0, 'INFO'=>'系统错误！', 'CODE'=>1);
			}
			else 
			{
				$this->_optComplete(true, '?m=account', '增加成功！');
			}
			$this->view->assign('AddInfo', $data);
			$this->view->assign('DO_STATUS', $doInfo);
		}
		$accounttypeModelCls = new AccounttypeModel();
		$this->view->assign('AccountType', $accounttypeModelCls->getAll());
		$organModelCls = new OrganModel($this->uid);
		$this->view->assign('Bank', $organModelCls->getBank());
		
		if($this->isAjax)
		{
		    $this->view->setUseLayout(false);
		}
		else
		{
		    $this->view->setPlace('content', 'page/account/add');
		}
		
		$this->view->display();
	}
	
	/**
	 * 修改账户
	 */
	public function edit()
	{
	    $do = Functions::gpc('do', 'p');
	    $accountID = null;
	    if($do == 'save') $accountID = Functions::sqlgpc('AccountID', 'p');
	    else $accountID = Functions::gpc('aid', 'g');
		$accountCls = new AccountModel($this->uid);
    	$accountInfo = $accountCls->getAccountByID(intval($accountID));
    	if(empty($accountInfo)){$this->_optComplete(false, '?m=account', '资产账户信息不存在！');}
	    if($do == 'save')
		{
		    $doInfo = array('SUCCESS'=>1, 'INFO'=>'', 'CODE'=>0);
			$data['AID'] = $accountID;
			$data['AccountName'] = Functions::sqlgpc('AccountName', 'p');
			$data['AccountType'] = Functions::sqlgpc('AccountType', 'p');
			$data['Bank'] = Functions::sqlgpc('Bank', 'p');
			$data['Remark'] = Functions::sqlgpc('Remark', 'p');
			$accountModelCls = new AccountModel($this->uid);
			if(!$accountModelCls->edit($data))
			{
			    $doInfo = array('SUCCESS'=>0, 'INFO'=>'系统错误！', 'CODE'=>1);
			    $accountInfo['AccountName'] = $data['AccountName'];
			    $accountInfo['AccountType'] = $data['AccountType'];
			    $accountInfo['Bank'] = $data['Bank'];
			    $accountInfo['Remark'] = $data['Remark'];
			}
			else
			{
				$this->_optComplete($doInfo['SUCCESS'], '?m=account', $doInfo['INFO']);
			}
			$this->view->assign('DO_STATUS', $doInfo);
		}
    	$this->view->assign('AccountInfo', $accountInfo);
    	$accounttypeModelCls = new AccounttypeModel();
    	$this->view->assign('AccountType', $accounttypeModelCls->getAll());
    	$organModelCls = new OrganModel($this->uid);
    	$this->view->assign('Bank', $organModelCls->getBank());
		if($this->isAjax)
		{
		    $this->view->setUseLayout(false);
		}
		else
		{
		    $this->view->setPlace('content', 'page/account/edit');
		}
		$this->view->display();
	}
	
	/**
	 * 注销账户
	 */
	public function cancel()
	{
		$accountID = Functions::gpc('aid', 'g');
		$confirm = Functions::gpc('confirm', 'g');
    	$accountCls = new AccountModel($this->uid);
    	$accountInfo = $accountCls->getAccountByID(intval($accountID));
    	if(empty($accountInfo)){$this->_redirect('?m=account');}
    	if($confirm == 'true')
    	{
    		$doInfo = array('SUCCESS'=>1, 'INFO'=>'', 'CODE'=>0);
    		if(!$accountCls->cancel($accountID))
    		{
    			$doInfo = array('SUCCESS'=>0, 'INFO'=>'系统错误', 'CODE'=>1);
    		}
    		$this->_optComplete($doInfo['SUCCESS'], '?m=account', $doInfo['INFO']);
    	}
    	$this->_askConfirm("?m=account&a=cancel&aid={$accountID}&confirm=true", '?m=account', '确认注销改账户？账户注销后账户内的资产将被重置为0！');
	}
	
	/**
	 * 调整余额（账户资产）
	 */
	public function adj()
	{
		$accountID = Functions::gpc('aid', 'g');
		$do = Functions::gpc('do', 'p');
    	$accountCls = new AccountModel($this->uid);
    	$accountInfo = $accountCls->getAccountByID(intval($accountID));
		if(empty($accountInfo)){$this->_redirect('?m=account');}
		if($accountInfo['Status'] != 1) {$this->_optComplete(false, '?m=account', '该账户未被启用');}
		
		if($do == 'save')
		{
			$doInfo = array('SUCCESS'=>1, 'INFO'=>'', 'CODE'=>0);
			$newTotalAssets = (float)Functions::sqlgpc('TotalAssets', 'p');
			if(!$accountCls->adj($accountID, $newTotalAssets, $accountInfo['TotalAssets']))
			{
				$doInfo = array('SUCCESS'=>0, 'INFO'=>'系统错误', 'CODE'=>1);
			}
			$this->_optComplete($doInfo['SUCCESS'], '?m=account', $doInfo['INFO']);
		}
		$this->view->assign('Account', $accountInfo);
		$this->view->setPlace('content', 'page/account/adj');
	}
	
	/**
	 * 恢复账户
	 */
	public function restore()
	{
		$accountID = Functions::gpc('aid', 'g');
		$accountCls = new AccountModel($this->uid);
		$account = $accountCls->getAccountByID($accountID);
		$doInfo = array('SUCCESS'=>1, 'INFO'=>'', 'CODE'=>0);
		if(empty($account) || !$accountCls->restore($accountID))
		{
			$doInfo = array('SUCCESS'=>0, 'INFO'=>'您指定的账户无法启用，该账户可能不存在！', 'CODE'=>1);
		}
		$this->_optComplete($doInfo['SUCCESS'], '?m=account', $doInfo['INFO']);
	}
	
	public function __destruct()
	{
		//echo 'index controller destruct!<br />';
	}
}